import { _decorator, Component, Node, Vec3 } from 'cc';
const { ccclass, property, executionOrder } = _decorator;



@ccclass('ToolMgr')
@executionOrder(-1)
export class ToolMgr extends Component {
    static Range(min: number, max: number): number {
        let len: number = max - min;
        let v: number = Math.random() * len;
        let res: number = Math.floor(v);
        return res + min;
    }

    static Range2(min: number, max: number): number {
        let len: number = max - min;
        let v: number = Math.random() * len;

        return v + min;
    }

    static RandomItem(arr) {
        let n = arr.length;
        let idx = ToolMgr.Range(0, n);
        return arr[idx];
    }

    static canHappen(val: number) {
        return Math.random() < val;
    }



    static Vec3Equal(v1: Vec3, v2: Vec3): boolean {
        return (v1.x === v2.x) && (v1.y === v2.y) && (v1.z === v2.z);
    }


    static DeepClone(): any {
        // 使用 JSON 方法作为降级方案
        return JSON.parse(JSON.stringify(this));
    }


    /**
     * 深拷贝
     * @param obj 对象
     */
    static DeepCopy(obj) {
        var result = Array.isArray(obj) ? [] : {}
        for (var key in obj) {
            if (obj.hasOwnProperty(key)) {
                if (typeof obj[key] === "object" && obj[key] !== null) {
                    result[key] = ToolMgr.DeepCopy(obj[key]) //递归复制
                } else {
                    result[key] = obj[key]
                }
            }
        }
        return result
    }
}


